package HomeWork.hw2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Administrator
 * @Date 2020/5/21 14:53
 **/
@WebServlet("/forwardAndRedirect1")
public class ForwardAndRedirect1 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*
        * 先定义已有的用户名和密码
        * 获取输入的用户名和密码
        * 比较，一致则重定向到forwardAndRedirect2
        *       不一致则到forwardAndRedirect3*/
        String name ="name";
        String pwd="123";
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        if (name.equals(username) &&pwd.equals(password)){
            //一致
            req.getSession().setAttribute("user",username);
            //重定向
            resp.sendRedirect("/web/forwardAndRedirect2");
        }else{
            //不一致

            resp.sendRedirect("/web/forwardAndRedirect3");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
